<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
$conf = new Config();
$rate0a_b = floatval( $conf->get( "rate0a_b" ) );
$rate0a_c = floatval( $conf->get( "rate0a_c" ) );
$rate0a_d = floatval( $conf->get( "rate0a_d" ) );
$rate1a_b = floatval( $conf->get( "rate1a_b" ) );
$rate1a_c = floatval( $conf->get( "rate1a_c" ) );
$rate1a_d = floatval( $conf->get( "rate1a_d" ) );
$ui = new ui();
$tpl = new Template2();
$fieldArr = array( "method" => "5", "gametype" => "0", "round" => "0", "lotto_id" => "0", "betcontent" => "", "orate" => "", "nrate" => "", "pagename" => "2", "paperopen" => date( "Y-m-d" ) );
$tpl->load( "main", "srate21.htm" );
$tpl->set( "cchinayear", intval( $conf->get( "chinayear" ) ) );
$num_order = "1";
if ( isset( $_GET['num_order'] ) )
{
    $num_order = $_GET['num_order'];
}
else if ( isset( $_POST['num_order'] ) )
{
    $num_order = $_POST['num_order'];
}
$disk = "a";
if ( isset( $_GET['disk'] ) )
{
    $disk = $_GET['disk'];
}
else if ( isset( $_POST['disk'] ) )
{
    $disk = $_POST['disk'];
}
include( "admin_menu.php" );
$orders = "<select onchange=\"location.replace('op.php?op=admin&fp=srate21&disk={$disk}&num_order='+this.value)\">";
$orders .= "<option value=1".( $num_order == "1" ? " selected" : "" ).">正码 一</option>";
$orders .= "<option value=2".( $num_order == "2" ? " selected" : "" ).">正码 二</option>";
$orders .= "<option value=3".( $num_order == "3" ? " selected" : "" ).">正码 三</option>";
$orders .= "<option value=4".( $num_order == "4" ? " selected" : "" ).">正码 四</option>";
$orders .= "<option value=5".( $num_order == "5" ? " selected" : "" ).">正码 五</option>";
$orders .= "<option value=6".( $num_order == "6" ? " selected" : "" ).">正码 六</option>";
$orders .= "</select>";
include( "rate_menu.php" );
srateMenu( "srate21", $disk, $orders );
$tpl->set( "action", "op.php?op=admin&fp=srate21&disk={$disk}&num_order={$num_order}" );
if ( $disk == "a" )
{
    $setRateFlag = true;
}
else
{
    $setRateFlag = false;
}
if ( ispost( ) )
{
    $fields = array();
    if ( $setRateFlag )
    {
        $sqlRate = "!!SELECT * FROM rate4{$num_order}a WHERE lotto_id = 0 LIMIT 0,1";
        $rsRate = getdb( $sqlRate );
        $setRateFlag = !$rsRate->eof;
    }
    $i = 1;
    while ( $i < 50 )
    {
        $ii = d2( $i );
        $temp = floatval( $_POST["r0_".$ii] );
        $idx = "r".$ii;
        $fields[$idx] = $temp;
        if ( $setRateFlag )
        {
            if ( $rsRate->f[$idx] != $temp )
            {
                $fieldArr['betcontent'] = GetBetContent( 25, "{$num_order} {$ii}" );
                $fieldArr['orate'] = $rsRate->f[$idx];
                $fieldArr['nrate'] = $temp;
                setRateRecord( $fieldArr );
            }
        }
        ++$i;
    }
    db_update( "rate4".$num_order.$disk, $fields, "lotto_id=0" );
    if ( $disk == "a" )
    {
        if ( $rate0a_b != 0 )
        {
            $fields = array();
            $i = 1;
            while ( $i < 50 )
            {
                $ii = d2( $i );
                $fields["r".$ii] = floatval( $_POST["r0_".$ii] ) - $rate0a_b;
                ++$i;
            }
            db_update( "rate4".$num_order."b", $fields, "lotto_id=0" );
        }
        if ( $rate0a_c != 0 )
        {
            $fields = array();
            $i = 1;
            while ( $i < 50 )
            {
                $ii = d2( $i );
                $fields["r".$ii] = floatval( $_POST["r0_".$ii] ) - $rate0a_c;
                ++$i;
            }
            db_update( "rate4".$num_order."c", $fields, "lotto_id=0" );
        }
        if ( $rate0a_d != 0 )
        {
            $fields = array();
            $i = 1;
            while ( $i < 50 )
            {
                $ii = d2( $i );
                $fields["r".$ii] = floatval( $_POST["r0_".$ii] ) - $rate0a_d;
                ++$i;
            }
            db_update( "rate4".$num_order."d", $fields, "lotto_id=0" );
        }
    }
}
$rs = getdb( "!!select * from rate4".$num_order.$disk." where lotto_id=0" );
if ( $rs->eof )
{
    getdb( "insert into rate4".$num_order.$disk." (lotto_id) values (0)" );
    $rs = getdb( "!!select * from rate4".$num_order.$disk." where lotto_id=0" );
}
$i = 1;
while ( $i < 50 )
{
    $ii = d2( $i );
    $tpl->set( "r0_".$ii, trimfloat( $rs->f["r".$ii], 1 ) );
    ++$i;
}
$rs->close();
$tpl->show( "main" );
?>